﻿YUI.add('cambiar-contrasenia-view', function (Y) {
    'use strict';
    Y.CambiarContrasenia = Y.Base.create('CambiarContrasenia', Y.View, [], {
        templateIsCharged: false,
        loadTemplate: function () {
            if (this.template == '') {
                var that = this,
                validator = null,
                request = Y.io('/ExcellentProject/scripts/views/seguridades/personas/templates/cambiarContrasenia.html', {
                    on: {
                        complete: function (id, response) {
                            that.templateIsCharged = true;
                            that.template = response.responseText;
                            that.render();
                        }
                    },
                    failure: function (id, response) {
                        alert(response)
                    }
                })
            }
        },

        render: function () {
            this.loadTemplate();
            if (this.templateIsCharged) {
                this.get('container').setHTML(this.template);
            }
        },

        events: {
            '.save': {
                click: 'cambiarContrasenia'
            }
        },

        cambiarContrasenia: function () {
            var config = {
                headers: Y.merge({
                    'Content-Type': 'application/json'
                }, amplify.store('authorization')),
                method: 'POST',
                data: JSON.stringify( {
                    OldPassword: Y.one('#ContraseniaActual').get('value'),
                    NewPassword: Y.one('#ContraseniaNueva').get('value'),
                    ConfirmPassword: Y.one('#ConfirmarContrasenia').get('value')
                }),
                on: {
                    success: function (id, response, d) {
                        bootbox.alert('Su Contraseña ha sido cambiada' + "\n" + 'Inicie sesión nuevamente',
                            function () {
                                window.location.replace("/excellentproject");
                            })

                    },
                    failure: function (id, response, d) {
                        var responseInformation = JSON.parse(response.responseText);
                        if (responseInformation.ModelState[""] != undefined) {
                            bootbox.alert("Contraseña Actual inválida");
                            return;
                        } else if (responseInformation.ModelState["model.ConfirmPassword"] != undefined) {
                            bootbox.alert("La nueva contraseña y su confirmación no coinciden.");
                            return;
                        } else {
                            bootbox.alert("La nueva contraseña no cumple los requisitos de complejidad del sistema")
                            return;
                        }
                    }
                }
            }

            Y.io('http://localhost/ExcellentProject.Seguridades/api/Account/ChangePassword',config)
        }
      
    })

}, '0.0.0.1', { requires: ['view', 'handlebars', 'node', 'event', 'node-event-simulate'] })
